<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        background-color: rgb(81, 15, 235);
        position: absolute;
        left: 0;
        top: 0;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <script>
      let box = document.querySelector(".box");
         box.addEventListener("mousedown", function (e) {
         let x=   e.pageX - this.offsetLeft;
          let y=  e.pageY - this.offsetTop;
           document.addEventListener("mousemove",move)
           function move(e){
             box.style.left = e.pageX - x  + "px";
            box.style.top = e.pageY  - y + "px";
           }
           document.addEventListener("mouseup", function () {
             document.removeEventListener("mousemove", move);
           });
         })
    </script>
  </body>
</html>
